﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SzCourt.Logic.Common;
using KMFrame.Core;
using SzCourt.Logic.NBL;
using NBearLite;
using SzCourt.Logic.NBL.DBEntities;
using System.Data;

namespace SzCourt.Logic.BLL
{
    public class CarryOut : SzCourtLogic
    {
        public PageResult GetCarryOutPage(CarryOutCondition condition)
        {
            PageResult result = new PageResult();

            ConditionCollection cdtColl = new ConditionCollection();
            ExpressionClip[] selectColumns =
            new ExpressionClip[] { 
                SZCourtColumns.pCaseCarryOutInfo.CCOIID,
                SZCourtColumns.pCaseCarryOutInfo.CaseName, 
                SZCourtColumns.pCaseCarryOutInfo.CauseOfAction,
                SZCourtColumns.pCaseCarryOutInfo.Plaintiff,
                SZCourtColumns.pCaseCarryOutInfo.Defendant,
                SZCourtColumns.pCaseCarryOutInfo.CourtClerk,
                SZCourtColumns.pCaseCarryOutInfo.Status,
            };
            cdtColl.SelectColumns = selectColumns;

            WhereClip where = WhereClip.All;
            if (condition.KeyWord != null && condition.KeyWord.Length > 0)
            {
                switch (condition.Type)
                {
                    case QueryType.Parties:
                        where = (SZCourtColumns.pCaseCarryOutInfo.Plaintiff.Contains(condition.KeyWord) || SZCourtColumns.pCaseCarryOutInfo.Defendant.Contains(condition.KeyWord));
                        break;
                    case QueryType.SrcCaseName:
                        where = SZCourtColumns.pCaseCarryOutInfo.SrcCaseName.Contains(condition.KeyWord);
                        break;
                    case QueryType.CaseSerial:
                        where = SZCourtColumns.pCaseCarryOutInfo.CaseSerial.Contains(condition.KeyWord);
                        break;
                    case QueryType.CaseName:
                        where = SZCourtColumns.pCaseCarryOutInfo.CaseName.Contains(condition.KeyWord);
                        break;
                }
            }
            cdtColl.Where = where;
            cdtColl.Select = NBLData.Select(SZCourtColumns.pCaseCarryOutInfo)
                .Where(where)
                .SetSelectRange(condition.PageSize, condition.PageSize * (condition.PageIndex - 1), SZCourtColumns.pCaseCarryOutInfo.CCOIID)
                .OrderBy(SZCourtColumns.pCaseCarryOutInfo.CaseSerial.Desc);

            List<CaseCarryOutItem> resultList = cdtColl.Select.ToList<CaseCarryOutItem>();
            List<pCaseCarryOutParties> carryOutPariesList = NBLData.Select(SZCourtColumns.pCaseCarryOutParties)
                    .Where(SZCourtColumns.pCaseCarryOutParties.CaseName.In(resultList.Select(a => a.CaseName).ToArray()))
                    .ToList<pCaseCarryOutParties>();
            foreach (CaseCarryOutItem item in resultList)
            {
                if (string.IsNullOrEmpty(item.Defendant) || string.IsNullOrEmpty(item.Plaintiff))
                {
                    foreach (pCaseCarryOutParties parties in carryOutPariesList)
                    {
                        if (item.CaseName == parties.CaseName)
                        {
                            if (int.Parse(parties.PartiesType) % 2 == 1)
                            {
                                item.Defendant = (string.IsNullOrEmpty(item.Defendant) ? "" : item.Defendant) + parties.PartiesName + ";";
                            }
                            else
                            {
                                item.Plaintiff = (string.IsNullOrEmpty(item.Plaintiff) ? "" : item.Plaintiff) + parties.PartiesName + ";";
                            }
                        }
                    }
                }
            }

            result.ResultList = resultList;
            int total = -1;
            if (condition.TotalCount == -1)
            {
                total = GetTotalRecord(SZCourtColumns.pCaseCarryOutInfo, SZCourtColumns.pCaseCarryOutInfo.CCOIID, where);
            }
            result.PageIndex = condition.PageIndex;
            result.PageSize = condition.PageSize;
            result.TotalCount = total;
            return result;
        }

        public CaseCarryInfo GetCarryOutInfo(pCaseCarryOutInfo condition)
        {
            CaseCarryInfo result = new CaseCarryInfo();
            //执行案件信息
            ConditionCollection cdtInfoColl = new ConditionCollection();
            if (condition.CCOIID <= 0)
                return result;
            WhereClip where = SZCourtColumns.pCaseCarryOutInfo.CCOIID == condition.CCOIID;

            cdtInfoColl.Where = where;
            cdtInfoColl.Select = NBLData.Select(SZCourtColumns.pCaseCarryOutInfo)
                .Where(where);
            pCaseCarryOutInfo info = cdtInfoColl.Select.ToSingleObject<pCaseCarryOutInfo>();
            if (info == null)
            {
                return result;
            }
            result.CaseName = info.CaseName;
            result.CourtClerk = info.CourtClerk;
            result.Judge = info.Judge;

            pCaseCarryOutParties carryOutParties = NBLData
               .Select(SZCourtColumns.pCaseCarryOutParties)
               .Where(SZCourtColumns.pCaseCarryOutParties.PWD == condition.QueryPWD && SZCourtColumns.pCaseCarryOutParties.CaseName == info.CaseName)
               .ToSingleObject<SzCourt.Logic.NBL.DBEntities.pCaseCarryOutParties>();
            //执行案件详细信息
            if (carryOutParties != null && carryOutParties.PWD == condition.QueryPWD)
            {
                result.QueryPWD = carryOutParties.PWD;

                List<pCaseCarryOutDetail> list = NBLData.Select(SZCourtColumns.pCaseCarryOutDetail)
                    .Where(SZCourtColumns.pCaseCarryOutDetail.CaseName == info.CaseName)
                    .ToList<pCaseCarryOutDetail>();
                result.detailList = list;


                ExpressionClip[] selectColumns =
           new ExpressionClip[] { 
                SZCourtColumns.pCaseCarryOutDoc.DocID,
                SZCourtColumns.pCaseCarryOutDoc.CaseName, 
                SZCourtColumns.pCaseCarryOutDoc.AddUserName,
                SZCourtColumns.pCaseCarryOutDoc.CauseOfAction,
                SZCourtColumns.pCaseCarryOutDoc.DocLookedUp,
                SZCourtColumns.pCaseCarryOutDoc.DocOriginal,
                SZCourtColumns.pCaseCarryOutDoc.DocPublishDate,
                SZCourtColumns.pCaseCarryOutDoc.DocSerial,
                SZCourtColumns.pCaseCarryOutDoc.IsRAR,
            };
                List<pCaseCarryOutDoc> doclist = NBLData.Select(SZCourtColumns.pCaseCarryOutDoc, selectColumns)
                  .Where(SZCourtColumns.pCaseCarryOutDoc.CaseName == info.CaseName)
                  .ToList<pCaseCarryOutDoc>();
                result.docList = doclist;
            }
            return result;
        }

        public pCaseCarryOutDoc GetCarryOutDoc(int DocID)
        {
            pCaseCarryOutDoc result = new pCaseCarryOutDoc();
            ExpressionClip[] selectColumns =
            new ExpressionClip[] { 
                SZCourtColumns.pCaseCarryOutDoc.DocID, 
                SZCourtColumns.pCaseCarryOutDoc.CaseName,
                SZCourtColumns.pCaseCarryOutDoc.CauseOfAction, 
                SZCourtColumns.pCaseCarryOutDoc.DocLookedUp,
                SZCourtColumns.pCaseCarryOutDoc.AddDate,
                SZCourtColumns.pCaseCarryOutDoc.CaseContent,
                SZCourtColumns.pCaseCarryOutDoc.DocSerial,
                SZCourtColumns.pCaseCarryOutDoc.IsRAR,
                SZCourtColumns.pCaseCarryOutDoc.DocOriginal,
            };

            DataSet ds = NBLData.Select(SZCourtColumns.pCaseCarryOutDoc)
                .Where(SZCourtColumns.pCaseCarryOutDoc.DocID == DocID)
                .ToDataSet();
            if(ds==null || ds.Tables.Count<=0)
            {
                return null;
            }
                if(ds.Tables[0]==null || ds.Tables[0].Rows.Count<=0)
                {
                     return null;
                }
            result.DocID=int.Parse(ds.Tables[0].Rows[0]["DocID"].ToString());
            result.CaseName=ds.Tables[0].Rows[0]["CaseName"].ToString();
            result.DocLookedUp=ds.Tables[0].Rows[0]["DocLookedUp"].ToString();
            result.CaseContent=(byte[])(ds.Tables[0].Rows[0]["CaseContent"]);

            return result;
        }
    }

    public class CarryOutCondition : PageParam
    {
        public string KeyWord { get; set; }
        public QueryType Type { get; set; }
    }

    public class CaseCarryOutItem
    {
        public int CCOIID { get; set; }
        public string CaseName { get; set; }
        public string CauseOfAction { get; set; }
        public string Plaintiff { get; set; }
        public string Defendant { get; set; }
        public string CourtClerk { get; set; }
        public string Status { get; set; }
    }

    public class CaseCarryInfo
    {
        public string CaseName { get; set; }
        public string Judge { get; set; }
        public string CourtClerk { get; set; }
        public string QueryPWD { get; set; }
        public List<pCaseCarryOutDetail> detailList { get; set; }
        public List<pCaseCarryOutDoc> docList { get; set; }
    }



    public enum QueryType
    {
        /// <summary>
        /// 按当事人查询
        /// </summary>
        Parties,
        /// <summary>
        /// 按原审案号查询
        /// </summary>
        SrcCaseName,
        /// <summary>
        /// 按流水号查询
        /// </summary>
        CaseSerial,
        /// <summary>
        /// 按执行案号查询
        /// </summary>
        CaseName,
    }
}
